﻿using System;
using System.Data;
using wojilu.Apps.Qycms.Domain;
using wojilu.Web.Mvc.Attr;
using YBBYunQi;

namespace wojilu.Web.Controller.Big5.Article
{
    public class ListController : BaseTempController
    {
        public ListController()
        {
            LayoutControllerType = typeof(Big5Controller);
        }

        [NonVisit]
        public virtual void setpublic()
        {
            set("top", loadHtml(new Big5.PublicController().Top));
            set("bottom", loadHtml(new Big5.PublicController().Bottom));
        }

        [HttpGet]
        public virtual void Show(Int64 Id)
        {
            var t = TypeList.findById(Id);
            if (ConfigUtil.GetConfigInt("is_html") == 0)
            {
                //跳转
                if (t.gotoid != 0)
                {
                    ctx.web.RedirectPermanent(to(Show, t.gotoid));
                    return;
                }
            }
            else
            {
                if (t.gotoid != 0)
                {
                    run(Show, t.gotoid);
                }
            }
            //模板
            var template = String.Empty;
            if (t.styleid == 4)
            {
                template = t.readtemplate;
            }
            else
            {
                if (t.styleid == 1)
                {
                    template = t.indextemplates;
                }
                else
                {
                    template = t.template;
                }
            }
            view(DefaultTemp + "Article/" + template);
            var read = new DataTable();
            //标题、关键词、描述
            var title = String.Empty;
            var Keywords = String.Empty;
            var Description = String.Empty;
            var Position = String.Empty;
            var titlelist = new YBBCommon().GetParentStr(Id, true);
            var tl = TypeList.find(" Id in (" + titlelist + ")").list();
            var i = 0;
            if (t.styleid == 4)
            {
                read = db.RunTable<Document>("select ybb_document.*,ybb_document_content.*,ybb_document_attr.* from ybb_document left join ybb_document_content on ybb_document.id=ybb_document_content.did left join ybb_document_attr on ybb_document.id=ybb_document_attr.did where ybb_document.id=" + t.linkid);
                var dtrow = read.Rows[0];
                set("read", dtrow);
                if (dtrow["headtitle"].IsNull())
                {
                    foreach (var row in tl)
                    {
                        i++;
                        if (i == 1)
                        {
                            title = read.Rows[0]["title"].ToString();
                        }
                        else
                        {
                            title += "-" + row.typename;
                        }
                    }
                    title += "-" + YBBCommon.GetLangStr(CurrentLng, "sitename");
                }
                else
                {
                    title += dtrow["headtitle"].ToString();
                }
                if (!dtrow["keywords"].IsNull())
                {
                    Keywords = dtrow["keywords"].ToString();
                }
                else
                {
                    Keywords = YBBCommon.GetLangStr(CurrentLng, "keyword");
                }
                if (!dtrow["description"].IsNull())
                {
                    Description = dtrow["description"].ToString();
                }
                else
                {
                    Description = YBBCommon.GetLangStr(CurrentLng, "description");
                }
            }
            else
            {
                foreach (var row in tl)
                {
                    i++;
                    if (i == 1)
                    {
                        title = row.typename;
                    }
                    else
                    {
                        title += "-" + row.typename;
                    }
                }
                title += "-" + YBBCommon.GetLangStr(CurrentLng, "sitename");
                Keywords = YBBCommon.GetLangStr(CurrentLng, "keyword");
                Description = YBBCommon.GetLangStr(CurrentLng, "description");

                var condition = String.Empty;
                var pagemax = t.pagemax;
                if (pagemax == 0)
                {
                    var m = Model.findById(t.mid);
                    pagemax = m.pagemax;
                }
                if (t.ispart == 1)
                {
                    titlelist = new YBBCommon().GetChildStr(Id, true);
                    var pagelist = Document.findPage(" tid in (" + titlelist + ")  or (extid like '%," + Id + ",%' or extid like '" + Id + ",%' or extid='" + Id + "' or extid like  '%," + Id + "') order by pid asc,addtime desc", pagemax);
                    set("pagelist", pagelist);
                    set("page", pagelist.PageBar);
                    pagelist.PageBar = new PageInfo().setPagerBar(pagelist.RecordCount, pagelist.PageCount, pagelist.Current, pagelist.Size, CurrentLng, t);
                    set("page", pagelist.PageBar);
                }
                else
                {
                    var pagelist = Document.findPage(" tid=" + Id + " or (extid like '%," + Id + ",%' or extid like '" + Id + ",%' or extid='" + Id + "' or extid like  '%," + Id + "') order by pid asc,addtime desc", pagemax);
                    set("pagelist", pagelist);
                    set("page", pagelist.PageBar);
                    pagelist.PageBar = new PageInfo().setPagerBar(pagelist.RecordCount, pagelist.PageCount, pagelist.Current, pagelist.Size, CurrentLng, t);
                    set("page", pagelist.PageBar);
                }
            }
            ctx.Page.SetTitle(title);
            ctx.Page.Keywords = Keywords;
            ctx.Page.Description = Description;

            var dlist = new YBBCommon().GetParentStr(Id, true);
            tl = TypeList.find(" Id in (" + dlist + ")").list();
            i = 0;
            foreach (var row in tl)
            {
                i++;
                if (i == 1)
                {
                    Position += "<span><a href=\"" + UrlLinkHelp.GetArticleLink(DefaultLng, CurrentLng, row.Id) + "\" >" + row.typename + "</a></span>";
                }
                else
                {
                    Position = "<a href=\"" + UrlLinkHelp.GetArticleLink(DefaultLng, CurrentLng, row.Id) + "\" >" + row.typename + "</a> &gt;&gt; " + Position;
                }
            }
            set("daohang", Position);

            m_prenext mp = new m_prenext();
            #region 下一篇
            var minid = db.RunScalar<TypeList>("select min(id) from ybb_typelist where id >" + Id + " and upid = " + t.upid).ObjectToInt();
            if (minid > 0)
            {
                var mt = TypeList.findById(minid);
                if (mt != null)
                {
                    mp.Name = mt.typename;
                    mp.Url = UrlLinkHelp.GetArticleLink(DefaultLng, CurrentLng, mt.Id);
                }
                else
                {
                    mp.Name = YBBCommon.GetLangStr(CurrentLng, "prenextno");
                    mp.Url = UrlLinkHelp.GetArticleLink(DefaultLng, CurrentLng, t.Id);
                }
            }
            else
            {
                mp.Name = YBBCommon.GetLangStr(CurrentLng, "prenextno");
                mp.Url = UrlLinkHelp.GetArticleLink(DefaultLng, CurrentLng, t.Id);
            }
            bind("pre", mp);
            #endregion
            #region 上一篇
            var maxid = db.RunScalar<TypeList>("select max(id) from ybb_typelist where id <" + Id + " and upid = " + t.upid).ObjectToInt();
            mp = new m_prenext();
            if (maxid > 0)
            {
                var mt = TypeList.findById(maxid);
                if (mt != null)
                {
                    mp.Name = mt.typename;
                    mp.Url = UrlLinkHelp.GetArticleLink(DefaultLng, CurrentLng, mt.Id);
                }
                else
                {
                    mp.Name = YBBCommon.GetLangStr(CurrentLng, "prenextno");
                    mp.Url = UrlLinkHelp.GetArticleLink(DefaultLng, CurrentLng, t.Id);
                }
            }
            else
            {
                mp.Name = YBBCommon.GetLangStr(CurrentLng, "prenextno");
                mp.Url = UrlLinkHelp.GetArticleLink(DefaultLng, CurrentLng, t.Id);
            }
            bind("next", mp);
            #endregion

            set("typeread", t);
            setpublic();
            SetPublic();
        }

        [HttpGet]
        public virtual void SearchGet(Int64 Id)
        {
            view(DefaultTemp + "Article/Search");

            //标题、关键词、描述
            var Title = String.Empty;
            var Keywords = String.Empty;
            var Description = String.Empty;
            Title += YBBCommon.GetLangStr(CurrentLng, "sitename");
            Keywords = YBBCommon.GetLangStr(CurrentLng, "keyword");
            Description = YBBCommon.GetLangStr(CurrentLng, "description");

            ctx.Page.SetTitleOnly(Title);
            ctx.Page.Keywords = Keywords;
            ctx.Page.Description = Description;

            var child = new YBBCommon().GetChildStr(Id, true);
            var t = TypeList.findById(Id);
            var pagemax = t.pagemax;
            if (pagemax == 0)
            {
                var m = Model.findById(t.mid);
                pagemax = m.pagemax;
            }
            var keyword = ctx.Get("keyword");
            var pagelist = Document.findPage(" tid in (" + child + ") and title like '%" + keyword + "%' order by pid asc,id desc", pagemax);
            set("pagelist", pagelist);
            set("page", pagelist.PageBar);

            setpublic();
            SetPublic();
        }

        [HttpGet]
        public virtual void SearchGet1(Int64 Id)
        {
            view(DefaultTemp + "Article/Search1");

            //标题、关键词、描述
            var Title = String.Empty;
            var Keywords = String.Empty;
            var Description = String.Empty;
            Title += YBBCommon.GetLangStr(CurrentLng, "sitename");
            Keywords = YBBCommon.GetLangStr(CurrentLng, "keyword");
            Description = YBBCommon.GetLangStr(CurrentLng, "description");

            ctx.Page.SetTitleOnly(Title);
            ctx.Page.Keywords = Keywords;
            ctx.Page.Description = Description;

            var child = new YBBCommon().GetChildStr(Id, true);
            var t = TypeList.findById(Id);
            var pagemax = t.pagemax;
            if (pagemax == 0)
            {
                var m = Model.findById(t.mid);
                pagemax = m.pagemax;
            }
            var keyword = ctx.Get("keyword");
            var pagelist = Document.findPage(" tid in (" + child + ") and title like '%" + keyword + "%' order by pid asc,id desc", pagemax);
            set("pagelist", pagelist);
            set("page", pagelist.PageBar);

            setpublic();
            SetPublic();
        }

    }
}
